﻿package com.mui.controls
{
    import com.gengine.core.*;
    import com.gengine.global.*;
    import com.mui.core.*;
    import com.mui.utils.*;
    import fl.controls.*;
    import fl.controls.listClasses.*;
    import fl.core.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;

    public class GList extends List implements IFrUI
    {
        public const CLASSNAME:String = "List";
        private var _verticalGap:Number = 0;
        private var _isDisposed:Boolean;
        private var _styleName:String;
        private var _isStyleChange:Boolean = false;

        public function GList()
        {
            this.tabChildren = false;
            this._styleName = this.CLASSNAME;
            return;
        }// end function

        public function set verticalGap(param1:Number) : void
        {
            if (this._verticalGap == param1)
            {
                return;
            }
            this._verticalGap = param1;
            invalidate();
            drawNow();
            return;
        }// end function

        public function get verticalGap() : Number
        {
            return this._verticalGap;
        }// end function

        final override protected function configUI() : void
        {
            super.configUI();
            this.createChildren();
            return;
        }// end function

        public function get styleName() : String
        {
            return this._styleName;
        }// end function

        public function set styleName(param1:String) : void
        {
            if (this._styleName != param1)
            {
                this._styleName = param1;
                invalidate(InvalidationType.STYLES);
                this._isStyleChange = true;
            }
            return;
        }// end function

        public function getCellRenderer(param1:Object) : ICellRenderer
        {
            var _loc_2:DisplayObject = null;
            if (param1 is Class)
            {
                _loc_2 = UICompomentPool.getUICompoment(param1 as Class) as DisplayObject;
                if (_loc_2 is IFrUIContainer)
                {
                    (_loc_2 as IFrUIContainer).createDisposedChildren();
                }
                return _loc_2 as ICellRenderer;
            }
            return getDisplayObjectInstance(param1) as ICellRenderer;
        }// end function

        public function disposeRender() : void
        {
            var _loc_1:ICellRenderer = null;
            if (_dataProvider)
            {
                _dataProvider.removeAll();
            }
            while (activeCellRenderers.length > 0)
            {
                
                _loc_1 = activeCellRenderers.pop();
                this.removeRenderEventListener(_loc_1 as Sprite);
                if (_loc_1 is IDispose)
                {
                    (_loc_1 as IDispose).dispose();
                    continue;
                }
                list.removeChild(_loc_1 as DisplayObject);
            }
            while (availableCellRenderers.length > 0)
            {
                
                _loc_1 = availableCellRenderers.pop();
                this.removeRenderEventListener(_loc_1 as Sprite);
                if (_loc_1 is IDispose)
                {
                    (_loc_1 as IDispose).dispose();
                    continue;
                }
                if ((_loc_1 as DisplayObject).parent)
                {
                    (_loc_1 as DisplayObject).parent.removeChild(_loc_1 as DisplayObject);
                }
            }
            return;
        }// end function

        protected function removeRenderEventListener(param1:Sprite) : void
        {
            if (!param1)
            {
                return;
            }
            param1.removeEventListener(MouseEvent.CLICK, handleCellRendererClick, false);
            param1.removeEventListener(MouseEvent.ROLL_OVER, handleCellRendererMouseEvent, false);
            param1.removeEventListener(MouseEvent.ROLL_OUT, handleCellRendererMouseEvent, false);
            param1.removeEventListener(Event.CHANGE, handleCellRendererChange, false);
            param1.doubleClickEnabled = false;
            param1.removeEventListener(MouseEvent.DOUBLE_CLICK, handleCellRendererDoubleClick, false);
            return;
        }// end function

        final override protected function draw() : void
        {
            if (isInvalid(InvalidationType.STYLES))
            {
                this.updateStyle();
            }
            if (isInvalid(InvalidationType.DATA))
            {
                this.updateDate();
            }
            if (isInvalid(InvalidationType.SIZE))
            {
                this.updateSize();
            }
            if (isInvalid(InvalidationType.SIZE, InvalidationType.SELECTED, InvalidationType.DATA))
            {
                this.updateDisplayList();
            }
            var _loc_1:* = rowHeight * length + this.verticalGap * length - 1;
            var _loc_2:* = contentHeight != _loc_1;
            contentHeight = _loc_1;
            if (isInvalid(InvalidationType.STYLES))
            {
                setStyles();
                drawBackground();
                if (contentPadding != getStyleValue("contentPadding"))
                {
                    invalidate(InvalidationType.SIZE, false);
                }
                if (_cellRenderer != getStyleValue("cellRenderer"))
                {
                    _invalidateList();
                    _cellRenderer = getStyleValue("cellRenderer");
                }
            }
            if (isInvalid(InvalidationType.SIZE, InvalidationType.STATE) || _loc_2)
            {
                drawLayout();
            }
            if (isInvalid(InvalidationType.RENDERER_STYLES))
            {
                updateRendererStyles();
            }
            if (isInvalid(InvalidationType.STYLES, InvalidationType.SIZE, InvalidationType.DATA, InvalidationType.SCROLL, InvalidationType.SELECTED))
            {
                this.drawList();
            }
            updateChildren();
            validate();
            return;
        }// end function

        override protected function drawList() : void
        {
            var _loc_5:uint = 0;
            var _loc_6:Object = null;
            var _loc_7:ICellRenderer = null;
            var _loc_10:Boolean = false;
            var _loc_11:String = null;
            var _loc_12:Object = null;
            var _loc_13:Sprite = null;
            var _loc_14:String = null;
            var _loc_1:* = rowHeight + this.verticalGap;
            var _loc_15:* = contentPadding;
            listHolder.y = contentPadding;
            listHolder.x = _loc_15;
            var _loc_2:* = listHolder.scrollRect;
            _loc_2.x = _horizontalScrollPosition;
            _loc_2.y = Math.floor(_verticalScrollPosition) % _loc_1;
            listHolder.scrollRect = _loc_2;
            listHolder.cacheAsBitmap = useBitmapScrolling;
            var _loc_3:* = Math.floor(_verticalScrollPosition / _loc_1);
            var _loc_4:* = Math.min(length, _loc_3 + rowCount + 1);
            var _loc_15:* = new Dictionary(true);
            renderedItems = new Dictionary(true);
            var _loc_8:* = _loc_15;
            _loc_5 = _loc_3;
            while (_loc_5 < _loc_4)
            {
                
                _loc_8[_dataProvider.getItemAt(_loc_5)] = true;
                _loc_5 = _loc_5 + 1;
            }
            var _loc_9:* = new Dictionary(true);
            while (activeCellRenderers.length > 0)
            {
                
                _loc_7 = activeCellRenderers.pop() as ICellRenderer;
                _loc_6 = _loc_7.data;
                if (_loc_8[_loc_6] == null || invalidItems[_loc_6] == true)
                {
                    availableCellRenderers.push(_loc_7);
                }
                else
                {
                    _loc_9[_loc_6] = _loc_7;
                    invalidItems[_loc_6] = true;
                }
                list.removeChild(_loc_7 as DisplayObject);
            }
            invalidItems = new Dictionary(true);
            _loc_5 = _loc_3;
            while (_loc_5 < _loc_4)
            {
                
                _loc_10 = false;
                _loc_6 = _dataProvider.getItemAt(_loc_5);
                if (_loc_9[_loc_6] != null)
                {
                    _loc_10 = true;
                    _loc_7 = _loc_9[_loc_6];
                    delete _loc_9[_loc_6];
                }
                else if (availableCellRenderers.length > 0)
                {
                    _loc_7 = availableCellRenderers.pop() as ICellRenderer;
                }
                else
                {
                    _loc_7 = this.getCellRenderer(getStyleValue("cellRenderer"));
                    _loc_13 = _loc_7 as Sprite;
                    if (_loc_13 != null)
                    {
                        _loc_13.addEventListener(MouseEvent.CLICK, handleCellRendererClick, false, 0, true);
                        _loc_13.addEventListener(MouseEvent.ROLL_OVER, handleCellRendererMouseEvent, false, 0, true);
                        _loc_13.addEventListener(MouseEvent.ROLL_OUT, handleCellRendererMouseEvent, false, 0, true);
                        _loc_13.addEventListener(Event.CHANGE, handleCellRendererChange, false, 0, true);
                        _loc_13.doubleClickEnabled = true;
                        _loc_13.addEventListener(MouseEvent.DOUBLE_CLICK, handleCellRendererDoubleClick, false, 0, true);
                        if (_loc_13["setStyle"] != null)
                        {
                            for (_loc_14 in rendererStyles)
                            {
                                
                                var _loc_17:* = _loc_13;
                                _loc_17._loc_13["setStyle"](_loc_14, rendererStyles[_loc_14]);
                            }
                        }
                    }
                }
                list.addChild(_loc_7 as Sprite);
                activeCellRenderers.push(_loc_7);
                _loc_7.x = 0;
                _loc_7.y = _loc_1 * (_loc_5 - _loc_3);
                _loc_7.setSize(availableWidth + _maxHorizontalScrollPosition, rowHeight);
                _loc_11 = this.itemToLabel(_loc_6);
                _loc_12 = null;
                if (_iconFunction != null)
                {
                    _loc_12 = _iconFunction(_loc_6);
                }
                else if (_iconField != null)
                {
                    if (_loc_6.hasOwnProperty(_iconField))
                    {
                        _loc_12 = _loc_6[_iconField];
                    }
                }
                if (!_loc_10)
                {
                    _loc_7.data = _loc_6;
                }
                _loc_7.listData = new ListData(_loc_11, _loc_12, this, _loc_5, _loc_5, 0);
                _loc_7.selected = _selectedIndices.indexOf(_loc_5) != -1;
                if (_loc_7 is UIComponent)
                {
                    (_loc_7 as UIComponent).drawNow();
                }
                _loc_5 = _loc_5 + 1;
            }
            return;
        }// end function

        override public function itemToLabel(param1:Object) : String
        {
            if (_labelFunction != null)
            {
                return String(_labelFunction(param1));
            }
            if (_labelField == null || param1 == null)
            {
                return "";
            }
            if (param1.hasOwnProperty(_labelField))
            {
                return param1[_labelField] != null ? (String(param1[_labelField])) : ("");
            }
            return "";
        }// end function

        protected function createChildren() : void
        {
            return;
        }// end function

        protected function updateStyle() : void
        {
            return;
        }// end function

        protected function updateSize() : void
        {
            return;
        }// end function

        protected function updateDate() : void
        {
            return;
        }// end function

        protected function updateDisplayList() : void
        {
            return;
        }// end function

        public function configEventListener(param1:String, param2:Function, param3:Boolean = false, param4:int = 0, param5:Boolean = false) : void
        {
            ObjEventListerTool.addObjEvent(this, param1, param2, param3);
            addEventListener(param1, param2, param3, param4, param5);
            return;
        }// end function

        public function set isDisposed(param1:Boolean) : void
        {
            this._isDisposed = param1;
            return;
        }// end function

        override public function removeEventListener(param1:String, param2:Function, param3:Boolean = false) : void
        {
            ObjEventListerTool.removeObjEvent(this, param1, param2, param3);
            super.removeEventListener(param1, param2, param3);
            return;
        }// end function

        public function dispose(param1:Boolean = true) : void
        {
            if (this._isDisposed && param1)
            {
                if (Global.isDebugModle)
                {
                    Alert.show("存在组件二次释放" + this.toString());
                }
                return;
            }
            this.disposeRender();
            renderedItems = new Dictionary(true);
            invalidItems = new Dictionary(true);
            _horizontalScrollPosition = 0;
            _verticalScrollPosition = 0;
            ObjEventListerTool.delObjEvent(this);
            if (this.parent)
            {
                this.parent.removeChild(this);
            }
            if (param1)
            {
                UICompomentPool.disposeUICompoment(this);
            }
            return;
        }// end function

    }
}
